package demo.util;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Utils {
	
	

	public static void printValueByReflet(Object obj){
		String value = "";
		try {
			Field[] fields = obj.getClass().getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);  
				System.out.print("| "+field.getName()+" : "+field.get(obj)+" |"  );
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void toPrintObject(Object obj){
		printValueByReflet(obj);
		System.out.println();
	}
	public static void toPrintList(List objs){
		for (Object obj : objs) {
			printValueByReflet(obj);
			System.out.println();
		}
	}
	
	public static int getUserType (String id){
		/*
		 * 25 admin a
		 * 26 teacher t
		 * 27 student s
		 * 28 test test
		 * */
		String sign = id.substring(0, 1);
		if (sign.equals("a")) {
			return 25;
		}else if(sign.equals("s")){
			return 27;
		}else if(sign.equals("t")){
			String test = id.substring(0, 4);
			if (test.equals("test")) {
				return 28;
			}else {
				return 26;
			}
		}else{
			return 28;
		}
	}
	
	/**
     * 根据名字获取cookie
     * 
     * @param request
     * @param name
     *            cookie名字
     * @return
     */
    public static Cookie getCookieByName(HttpServletRequest request, String name) {
        Map<String, Cookie> cookieMap = ReadCookieMap(request);
        if (cookieMap.containsKey(name)) {
            Cookie cookie = (Cookie) cookieMap.get(name);
            return cookie;
        } else {
            return null;
        }
    }

    /**
     * 将cookie封装到Map里面
     * 
     * @param request
     * @return
     */
    private static Map<String, Cookie> ReadCookieMap(HttpServletRequest request) {
        Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }

    /**
     * 保存Cookies
     * 
     * @param response
     *            servlet请求
     * @param value
     *            保存值
     * @author jxf
     */
    public static HttpServletResponse setCookie(HttpServletResponse response, String name, String value,int time) {
        // new一个Cookie对象,键值对为参数
        Cookie cookie = new Cookie(name, value);
        // tomcat下多应用共享
        cookie.setPath("/");
        // 如果cookie的值中含有中文时，需要对cookie进行编码，不然会产生乱码
        try {
            URLEncoder.encode(value, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        cookie.setMaxAge(time);
        // 将Cookie添加到Response中,使之生效
        response.addCookie(cookie); // addCookie后，如果已经存在相同名字的cookie，则最新的覆盖旧的cookie
        return response;
    }

}
